package com.saihai.jiankang.ufJdtgl.controller;

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.saihai.common.annotation.Log;
import com.saihai.common.core.controller.BaseController;
import com.saihai.common.core.domain.AjaxResult;
import com.saihai.common.enums.BusinessType;
import com.saihai.jiankang.ufJdtgl.domain.UfJdtgl;
import com.saihai.jiankang.ufJdtgl.service.IUfJdtglService;
import com.saihai.common.utils.poi.ExcelUtil;
import com.saihai.common.core.page.TableDataInfo;

/**
 * 轮播图Controller
 *
 * @author ruoyi
 * @date 2024-08-08
 */
@RestController
@RequestMapping("/jiankang/ufJdtgl")
public class UfJdtglController extends BaseController {
    @Autowired
    private IUfJdtglService ufJdtglService;

    /**
     * 查询轮播图列表
     */
    @GetMapping("/bannerListJZD")
    public TableDataInfo bannerListJZD(UfJdtgl ufJdtgl) {
        ufJdtgl.setLy(1L);
        ufJdtgl.setZt(0L);
        List<UfJdtgl> list = ufJdtglService.selectUfJdtglList(ufJdtgl);
        return getDataTable(list);
    }


    /**
     * 查询轮播图列表
     */
    @GetMapping("/bannerListYSD")
    public TableDataInfo bannerListYSD(UfJdtgl ufJdtgl) {
        ufJdtgl.setLy(0L);
        ufJdtgl.setZt(0L);
        List<UfJdtgl> list = ufJdtglService.selectUfJdtglList(ufJdtgl);
        return getDataTable(list);
    }

    /**
     * 导出轮播图列表
     */
    @Log(title = "轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UfJdtgl ufJdtgl) {
        List<UfJdtgl> list = ufJdtglService.selectUfJdtglList(ufJdtgl);
        ExcelUtil<UfJdtgl> util = new ExcelUtil<UfJdtgl>(UfJdtgl.class);
        util.exportExcel(response, list, "轮播图数据");
    }

    /**
     * 获取轮播图详细信息
     */
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ufJdtglService.selectUfJdtglById(id));
    }

    public static void addSuffixAndConvert(String sourceFilePath) {
        File sourceFile = new File(sourceFilePath);
        String destinationFilePath = sourceFilePath + ".jpg";

        try (FileInputStream fis = new FileInputStream(sourceFile);
             FileOutputStream fos = new FileOutputStream(destinationFilePath)) {

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

        } catch (IOException e) {
            System.err.println("文件出错");
            e.printStackTrace();
        }
    }

    /**
     * 获取轮播图详细信息
     */
    @GetMapping(value = "/selectImageFile/{id}")
    public void selectImageFile(@PathVariable("id") Long id, HttpServletResponse response) throws ServletException, IOException {
        UfJdtgl ufJdtgl = ufJdtglService.selectImageFile(id);
//        String sourceFilePath = "D:\\image\\3d59f59e-e633-4fd3-be0f-eaa67801879e";
        String sourceFilePath = ufJdtgl.getTp();
        addSuffixAndConvert(sourceFilePath);

        File file = new File(sourceFilePath + ".jpg");

        if (file.exists()) {
            response.setContentType("image/jpeg");

            try (FileInputStream fis = new FileInputStream(file);
                 OutputStream os = response.getOutputStream()) {

                byte[] buffer = new byte[1024];
                int len;
                while ((len = fis.read(buffer)) > 0) {
                    os.write(buffer, 0, len);
                }
            }
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }


//    /**
//     * 新增轮播图
//     */
//    @Log(title = "轮播图", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody UfJdtgl ufJdtgl) {
//        return toAjax(ufJdtglService.insertUfJdtgl(ufJdtgl));
//    }
//
//    /**
//     * 修改轮播图
//     */
//    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody UfJdtgl ufJdtgl) {
//        return toAjax(ufJdtglService.updateUfJdtgl(ufJdtgl));
//    }
//
//    /**
//     * 删除轮播图
//     */
//    @Log(title = "轮播图", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(ufJdtglService.deleteUfJdtglByIds(ids));
//    }
}
